package concurrensy.sin_array_sumator;

public class BadConcurrentSinArraySumator implements SinArraySumator {

	private class SinPositiveCounter implements Runnable {

		@Override
		public void run() {
			result += Math.sin(value);
		}

	}

	private class SinNegativeCounter implements Runnable {

		@Override
		public void run() {
			result += Math.sin(-value);
		}

	}

	private volatile double result;
	private int value;
	private Thread negativeThread;
	private Thread positiveThread;

	@Override
	public double arraySum(int parameter) {
		value = parameter;
		for (; value > 0; value--) {
			negativeThread = new Thread(new SinNegativeCounter());
			positiveThread = new Thread(new SinPositiveCounter());
			try {
				negativeThread.start();
				negativeThread.join();
				positiveThread.start();
				positiveThread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
		return result;
	}

}
